<template>
  <div class="app-container">
    <!-- 对象搜索 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="插件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入插件名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="status">
        <y-select
          v-model="queryParams.creator"
          placeholder="请选择创建人"
          :optionsFunc="listUser"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 对象操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Back" @click="handleBack"
          >返回</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Upload"
          @click="handleUpload"
          v-hasPermi="['tms:jobPlugins:upload']"
          >上传</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tms:jobPlugins:delete']"
          >删除</el-button
        >
      </el-col>
      <y-right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <!-- 对象列表 -->
    <el-table
      ref="tableRef"
      v-loading="loading"
      row-key="id"
      :data="pluginList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        reserve-selection
      />
      <el-table-column label="序号" width="50" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="编号"
        width="300"
        align="center"
        prop="id"
        :show-overflow-tooltip="true"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="插件名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
        v-if="columns[1].visible"
      />
      <el-table-column
        label="插件地址"
        align="center"
        prop="file"
        :show-overflow-tooltip="true"
        v-if="columns[2].visible"
      />
      <el-table-column
        label="插件MD5"
        align="center"
        prop="md5sum"
        :show-overflow-tooltip="true"
        v-if="columns[3].visible"
      />
      <el-table-column
        label="插件大小"
        align="center"
        prop="size"
        :show-overflow-tooltip="true"
        v-if="columns[4].visible"
      >
        <template #default="scope">
          {{ `${scope.row.size}(字节)` }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        prop="creator_name"
        v-if="columns[5].visible"
      />
      <el-table-column
        label="上传时间"
        align="center"
        prop="create_time"
        width="180"
        v-if="columns[6].visible"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" with="100" align="center">
        <template #default="scope">
          <el-tooltip content="预览" placement="top">
            <el-button
              link
              type="primary"
              icon="View"
              @click="handleScript(scope.row)"
              v-hasPermi="['tms:jobPlugins:view']"
            />
          </el-tooltip>
          <el-tooltip content="下载" placement="top">
            <el-button
              link
              type="primary"
              icon="Download"
              @click="handleDownLoad(scope.row)"
              v-hasPermi="['tms:jobPlugins:download']"
            />
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['tms:jobPlugins:delete']"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <y-file-upload
      v-model="open"
      url="/tms/job/plugin/"
      :title="title"
      @submited="submited"
      @close="open = false"
      :fileType="'.py'"
      :limit="10"
      :fileSize="10"
    />
    <y-file-preview
      v-model="preview"
      :file="file"
      :title="title"
      @close="preview = false"
    >
    </y-file-preview>
  </div>
</template>

<script setup name="JobPlugin">
import { listJobPlugin, delJobPlugin } from "@/api/tms/job/plugin";
import { listUser } from "@/api/sys/user";
import YFilePreview from "@/components/YFilePreview";

const { proxy } = getCurrentInstance();
const route = useRoute();

const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const preview = ref(false);
const file = ref("");
const pluginList = ref([]);

// 列显隐信息
const columns = ref([
  { key: 0, label: `编号`, visible: false },
  { key: 1, label: `插件名称`, visible: true },
  { key: 2, label: `插件地址`, visible: false },
  { key: 3, label: `插件MD5`, visible: true },
  { key: 4, label: `插件大小`, visible: true },
  { key: 5, label: `创建人`, visible: true },
  { key: 6, label: `上传时间`, visible: true },
]);

const data = reactive({
  form: {},
  queryParams: {
    job: route.query.job,
    name: undefined,
    status: undefined,
    page: 1,
    limit: 10,
  },
});

const { queryParams } = toRefs(data);

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listJobPlugin(queryParams.value).then((response) => {
    pluginList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

/** 返回按钮 */
function handleBack() {
  proxy.$router.back();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 上传按钮操作 */
function handleScript(row) {
  preview.value = true;
  file.value = row.file;
  title.value = "插件预览";
}

/** 上传按钮操作 */
function handleUpload() {
  open.value = true;
  title.value = "插件上传";
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    if (row.id) {
      await proxy.$modal.confirm(`确认是否删除 "${row.name}" ？`);
    } else {
      await proxy.$modal.confirm("确认是否删除所有已勾选信息？");
    }
    ids.value = row.id ? [row.id] : ids.value;
    await delJobPlugin(ids.value);
    proxy.$modal.msgSuccess("删除成功");
    proxy.$refs.tableRef.clearSelection();
    getList();
  } catch (error) {
    console.error(error);
  }
}

onMounted(() => {
  getList();
});

/** 下载按钮操作 */
function handleDownLoad(row) {
  let file = row.file;
  proxy.download("tms/job/plugin/download/", row.name, {
    file,
  });
}

/** submited事件 */
function submited() {
  getList();
}
</script>
